package com.simpleaccount.User;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.simpleaccount.Login.LoginActivity;
import com.simpleaccount.R;
import com.simpleaccount.SettingActivity;
import com.simpleaccount.Toast.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import static com.simpleaccount.R.id.btn_toLogin;

public class UserFragment extends Fragment {

    @Bind(R.id.tv_usercenter)
    TextView tvUsercenter;
    @Bind(btn_toLogin)
    RelativeLayout btnToLogin;
    @Bind(R.id.btn_userMsg)
    RelativeLayout btnUserMsg;
    @Bind(R.id.btn_setting)
    RelativeLayout btnSetting;
    @Bind(R.id.btn_shareApp)
    RelativeLayout btnShareApp;
    @Bind(R.id.btn_aboutUs)
    RelativeLayout btnAboutUs;
    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.ll_bottom_user)
    LinearLayout llBottomUser;

    @Bind(R.id.tv_userName)
    TextView tvUserName;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //fragment_user为fragment当前布局
        View tologin = inflater.inflate(R.layout.fragment_user, null);
        ButterKnife.bind(this, tologin);

        AppUser currentUser = BmobUser.getCurrentUser(AppUser.class);
        if (currentUser == null) { //判断是否登录
            tvUserName.setText("未登录");
            ivHead.setImageResource(R.drawable.headshow);
        } else {
            tvUserName.setText(currentUser.getNickName().toString());
            ivHead.setImageResource(R.drawable.head_show);
        }

        return tologin;

    }

    @Override

    public void onResume(){
        super.onResume();
        AppUser currentUser = BmobUser.getCurrentUser(AppUser.class);
        if (currentUser == null) { //判断是否登录
            tvUserName.setText("未登录");
            ivHead.setImageResource(R.drawable.headshow);
        }else{
            tvUserName.setText(currentUser.getNickName());
            ivHead.setImageResource(R.drawable.head_show);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        AppUser currentUser = BmobUser.getCurrentUser(AppUser.class);
        if (currentUser == null) { //判断是否登录
            tvUserName.setText("未登录");
            ivHead.setImageResource(R.drawable.headshow);
        }else{
            tvUserName.setText(currentUser.getNickName());
            ivHead.setImageResource(R.drawable.head_show);
        }
    }

    @OnClick({btn_toLogin, R.id.btn_userMsg, R.id.btn_setting, R.id.btn_aboutUs, R.id.btn_shareApp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case btn_toLogin:
                BmobUser currentUser = BmobUser.getCurrentUser();
                if (currentUser == null) { //判断是否登录
                    Intent intent_tologin = new Intent();
                    intent_tologin.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent_tologin);

                    break;
                } else
                    break;

            case R.id.btn_userMsg:      //个人信息
                BmobUser fUser = BmobUser.getCurrentUser();
                if (fUser == null) { //判断用户是否登录
                    ToastUtils.toast(getActivity(),"请登录","info");
                    break;
                } else {
                    Intent intent_userMsg = new Intent();
                    intent_userMsg.setClass(getActivity(), UserMessageActivity.class);
                    startActivity(intent_userMsg);
                    break;
                }
            case R.id.btn_setting:    //设置
                Intent intent_setting = new Intent();
                intent_setting.setClass(getActivity(), SettingActivity.class);
                startActivity(intent_setting);
                break;


            case R.id.btn_aboutUs:    //关于我们
                Intent intent_aboutUs = new Intent();
                intent_aboutUs.setClass(getActivity(), AboutUsActivity.class);
                startActivity(intent_aboutUs);
                break;

            case R.id.btn_shareApp:   //分享APP
                Intent intent_shareApp = new Intent();
                intent_shareApp.setClass(getActivity(), ShareAppActivity.class);
                startActivity(intent_shareApp);
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        ToastUtils.cancel();
    }

    @Override
    public void onStop() {
        super.onStop();
        ToastUtils.cancel();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        ToastUtils.cancel();
    }

}














